// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Calculator.java

package com.hdy.calculator;

import java.io.PrintStream;

// Referenced classes of package com.hdy.calculator:
//            calculateException

public class Calculator
{

    public Calculator()
    {
    }

    public double calculate(String calculation)
    {
        StringBuffer stringBuffer = new StringBuffer(calculation);
        StringBuffer stringBuffer1 = removeBracketParallel(stringBuffer);
        return doCal(stringBuffer1);
    }

    public double removeBracketInside(StringBuffer sb)
    {
        int first = searchFrontB(sb);
        int sec = searchBackB(sb, first);
        if(first != sb.length())
        {
            StringBuffer subSb = new StringBuffer(sb.substring(first + 1, sec));
            double result = removeBracketInside(subSb);
            sb.delete(first, sec + 1);
            sb.insert(first, result);
            StringBuffer sb2 = removeBracketParallel(sb);
            return doCal(sb2);
        } else
        {
            return doCal(sb);
        }
    }

    public StringBuffer removeBracketParallel(StringBuffer stringBuffer)
    {
        int length = stringBuffer.length();
        int first = searchFrontB(stringBuffer);
        do
        {
            if(first >= length)
                break;
            length = stringBuffer.length();
            double i = removeBracketInside(stringBuffer);
            first = searchFrontB(stringBuffer);
            int sec = searchBackB(stringBuffer, first);
            if(first == stringBuffer.length())
            {
                stringBuffer.delete(0, stringBuffer.length());
                stringBuffer.append(i);
                break;
            }
            stringBuffer.delete(first, sec + 1);
            stringBuffer.insert(first, i);
            first += Double.toString(i).length();
            int j = first;
            while(j < searchFrontB(stringBuffer)) 
            {
                if(stringBuffer.charAt(j) == ')')
                {
                    try
                    {
                        throw new calculateException("\u62EC\u53F7\u5F02\u5E38\uFF01");
                    }
                    catch(calculateException e)
                    {
                        System.out.println(e.getMessage());
                    }
                    System.exit(0);
                }
                j++;
            }
        } while(true);
        for(int i = 0; i < stringBuffer.length() && stringBuffer.charAt(i) != '('; i++)
        {
            if(stringBuffer.charAt(i) != ')')
                continue;
            try
            {
                throw new calculateException("\u62EC\u53F7\u5F02\u5E38\uFF01");
            }
            catch(calculateException e)
            {
                System.out.println(e.getMessage());
            }
            System.exit(0);
        }

        return stringBuffer;
    }

    public StringBuffer do1(StringBuffer sb1)
    {
        StringBuffer sb = new StringBuffer(sb1);
        for(int i = 0; i < sb.length(); i++)
        {
            String s1;
            String s2;
            if('*' == sb.charAt(i))
            {
                s1 = extractFront(sb, i);
                s2 = extractBack(sb, i);
                double val = Double.parseDouble(s1) * Double.parseDouble(s2);
                sb.delete(i - s1.length(), i + 1 + s2.length());
                sb.insert(i - s1.length(), val);
                continue;
            }
            if('/' != sb.charAt(i))
                continue;
            s1 = extractFront(sb, i);
            s2 = extractBack(sb, i);
            try
            {
                double val = Double.parseDouble(s1) / Double.parseDouble(s2);
                sb.delete(i - s1.length(), i + 1 + s2.length());
                sb.insert(i - s1.length(), val);
            }
            catch(calculateException e)
            {
                System.out.println(e.getMessage());
            }
        }

        return sb;
    }

    public double do2(StringBuffer sb1)
    {
        double result = 0.0D;
        StringBuffer sb = new StringBuffer(sb1);
        sb.append("$");
        int j = 0;
        if(sb.charAt(0) != '-')
            sb.insert(0, "+");
        for(int i = 1; i < sb.length(); i++)
        {
            if('+' != sb.charAt(i) && '-' != sb.charAt(i) && '$' != sb.charAt(i))
                continue;
            String substring = sb.substring(j + 1, i);
            if(sb.charAt(j) == '+')
            {
                result += Double.parseDouble(substring);
                j = i;
                continue;
            }
            if(sb.charAt(j) == '-')
            {
                result -= Double.parseDouble(substring);
                j = i;
            }
        }

        return result;
    }

    private String extractFront(StringBuffer sb1, int i)
    {
        StringBuffer sb = new StringBuffer(sb1);
        if(sb.charAt(0) != '-')
        {
            sb.insert(0, '+');
            i++;
        }
        int j;
        for(j = i; j != 0 && sb.charAt(j) != '+' && sb.charAt(j) != '-'; j--);
        if(j == i - 1)
        {
            try
            {
                throw new calculateException("\u7B26\u53F7\u5F02\u5E38\uFF01");
            }
            catch(calculateException e)
            {
                System.out.println(e.getMessage());
            }
            System.exit(0);
        }
        return sb.substring(j + 1, i);
    }

    private String extractBack(StringBuffer sb, int i)
    {
        int j = i + 1;
        do
        {
            if(j == sb.length())
                break;
            if(sb.charAt(j) == '-' && j == i + 1)
                j++;
            if(sb.charAt(j) == '+' || sb.charAt(j) == '/' || sb.charAt(j) == '*' || sb.charAt(j) == '-')
                break;
            j++;
        } while(true);
        if(j == i + 1)
        {
            try
            {
                throw new calculateException("\u7B26\u53F7\u5F02\u5E38\uFF01");
            }
            catch(calculateException e)
            {
                System.out.println(e.getMessage());
            }
            System.exit(0);
        }
        return sb.substring(i + 1, j);
    }

    private int searchFrontB(StringBuffer sb)
    {
        int first = 0;
        for(int length = sb.length(); first < length && sb.charAt(first) != '('; first++);
        return first;
    }

    private int searchBackB(StringBuffer sb, int first)
    {
        int sec = first + 1;
        int count = 1;
        if(first != sb.length())
        {
            do
            {
                if(count == 0 || sec >= sb.length())
                    break;
                if(sb.charAt(sec) == '(')
                {
                    count++;
                    sec++;
                } else
                if(sb.charAt(sec) == ')')
                {
                    if(--count != 0)
                        sec++;
                } else
                {
                    sec++;
                }
            } while(true);
            if(count != 0)
            {
                try
                {
                    throw new calculateException("\u62EC\u53F7\u5F02\u5E38\uFF01");
                }
                catch(calculateException e)
                {
                    System.out.println(e.getMessage());
                }
                System.exit(0);
            }
        }
        return sec;
    }

    private double doCal(StringBuffer sb1)
    {
        StringBuffer sb = new StringBuffer(sb1);
        StringBuffer s = do1(sb);
        return do2(s);
    }
}
